package com.hxzy.action;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.hxzy.entity.User;
import com.hxzy.service.AttachmentService;
import com.hxzy.service.AttachmentServiceImpl;
import com.hxzy.service.ServiceResult;
import com.hxzy.service.UserService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class RegisterAction extends AbstractAction{

	private static final long serialVersionUID = 3724103547191217021L;
	private String fileContentType;
	private AttachmentService attachmentService=new AttachmentServiceImpl();
	private UserService userService;
	private User user;
	ServiceResult<User> user1;
	
/*	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		User userInSession=(User) ActionContext.getContext().getSession().get("userRegister");
		if(userInSession!=null){
			user=userInSession;
		}
	}
	
	*/
	// 注册页面1，账号信息（邮箱，密码）
/*	public String post() throws Exception {
		OutputStream os = ServletActionContext.getResponse().getOutputStream();
		if(registerService.emailExists(user.getEmail())){
	        os.write(new String("邮箱已存在").getBytes());
	        os.flush();
	        os.close();
			return null;
		}else{
			os.write(new String("邮箱可使用").getBytes());
			os.flush();
	        os.close();
			if (user != null && !StringUtils.isBlank(user.getPasswd()) && user.getPasswd().equals(user.getPasswd2())) {
				Map<String, Object> session = ActionContext.getContext().getSession();
				session.put("userRegister", user);
				return "register2";
			}if(user != null &&!user.getPasswd().equals(user.getPasswd2())){
				addActionError("两次密码不一致");
				return "register1";
			}
		}
		return null;
	}*/
	public String register1() throws Exception {
		//OutputStream os = ServletActionContext.getResponse().getOutputStream();
	    if(isGet()){
	        return INPUT;
	    }
		User userEmail=userService.emailExists(user.getEmail());
		if(userEmail!=null){
	      //  os.write(new String("邮箱已存在").getBytes());
	     //   os.flush();
	       // os.close();
		    addActionError("邮箱已存在");
			return INPUT;
		}else{
			//os.write(new String("邮箱可使用").getBytes());
			//os.flush();
	       // os.close();
		    addActionError("邮箱可使用");
			if (user != null && !StringUtils.isBlank(user.getPasswd()) && user.getPasswd().equals(user.getPasswd2())) {
				Map<String, Object> session = ActionContext.getContext().getSession();
				session.put("userRegister", user);
				return "register2";
			}if(user != null &&!user.getPasswd().equals(user.getPasswd2())){
				addActionError("两次密码不一致");
				return INPUT;
			}
		}
		return INPUT;
	}

/*	public void validateRegister1() {
		if (user != null && StringUtils.isBlank(user.getEmail())) {
			addFieldError("email", "email不能为空");
		}
		if (user != null && StringUtils.isBlank(user.getPasswd())) {
			addFieldError("passwd", "密码不能为空");
		}
		if (user != null && StringUtils.isBlank(user.getPasswd2())) {
			addFieldError("passwd2", "请再次输入密码");
		}
	}
*/
	// 注册页面2，昵称，姓名，电话
	public String register2() {
		/*
		 * boolean flag = false; String check="^[1][3,4,5,7,8][0-9]{9}$";
		 * Pattern regex=Pattern.compile(check); Matcher matcher =
		 * regex.matcher(user.getPhone()); flag = matcher.matches(); if (flag ==
		 * false) { addActionError( "phone格式不正确"); }else{
		 */
		User userInSession = (User) ActionContext.getContext().getSession().get("userRegister");
		boolean isExsits=userService.nameExists(user.getUsername());
		if(isExsits){
			addActionError("用户名重复");
			return INPUT;
		}else{
			userInSession.setNickname(user.getNickname());
			userInSession.setUsername(user.getUsername());
			userInSession.setPhone(user.getPhone());
			ActionContext.getContext().getSession().put("userRegister", userInSession);
			return "register3";
		}
	}

	public String register3() {
		if (user != null &&!StringUtils.isBlank(user.getIdentify()) && "9479".equals(user.getIdentify())) {
			User userInSession1 = (User) ActionContext.getContext().getSession().get("userRegister");
			user1 = userService.register(userInSession1.getEmail(),userInSession1.getPhone(), userInSession1.getUsername(), userInSession1.getPasswd(), 
					userInSession1.getNickname(),userInSession1.getRealname(),userInSession1.getHeadImg());
			return Action.SUCCESS;
		}if (user != null &&!StringUtils.isBlank(user.getIdentify()) && !"9479".equals(user.getIdentify())) {
		addActionError("验证码输入有误");
		return INPUT;
		}
		return INPUT;
	}

	public void validateRegister3() {
		if (user != null &&StringUtils.isBlank(user.getIdentify())) {
			addFieldError("identify", "identify不能为空");
		}
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public AttachmentService getAttachmentService() {
		return attachmentService;
	}

	public void setAttachmentService(AttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}

    public ServiceResult<User> getUser1() {
        return user1;
    }

    public void setUser1(ServiceResult<User> user1) {
        this.user1 = user1;
    }


	

}
